/*
 * @Description: 走高德协议（传入关键字，页数，页码）
 * @Author: buwei.huang
 * @Date: 2019-07-25 14:01:23
 * @Last Modified by: buwei.huang
 * @Last Modified time: 2019-08-28 19:05:33
 */
import AMap from 'AMap'

function getAssociateListByAmap(con, pageIndex, pageSize) {
  return new Promise((resolve, reject) => {
    // alert('window.bus_amap_city' + window.bus_amap_city)
    AMap.service(['AMap.PlaceSearch'], function() {
      var placeSearch = new AMap.PlaceSearch({
        // 构造地点查询类
        city: window.bus_amap_city, // 城市
        citylimit: true,
        pageIndex: pageIndex,
        pageSize: pageSize
      })
      // 关键字查询
      placeSearch.search(con, (status, result) => {
        console.log('~~~~~~~getAssociateList+status~~~~~~~', status)
        console.log('~~~~~~~getAssociateList result:' + JSON.stringify(result))
        if (result.poiList) {
          resolve(result)
          return
        }
        parseResponseInfo(resolve, reject, status, result)
      })
    })
  })
}

// 通用的结果处理函数
function parseResponseInfo(resolve, reject, status, result) {
  if (status === 'complete' && result.info === 'OK') {
    // 成功处理
    // console.log(JSON.stringify(result))
    resolve(result)
  } else if (status === 'no_data' || result.info === 'NO_DATA') {
    reject('no_data')
  } else {
    // 失败处理
    var error = result.info ? result.info : result
    // 统一toast错误提示
    // vue.$toast(error)
    console.error('错误信息:' + error)
    console.error('错误信息result:' + JSON.stringify(result))
    reject(error)
  }
}

export { getAssociateListByAmap }
